프롬프트에서 파이프라인으로
LLM 상호작용의 진화
이전 수업에서는 단일 프롬프트 상호작용에 초점을 맞췄습니다. 그러나 현실 세계의 응용에서는 단순한 한 번의 질문과 답변 이상이 필요합니다. 확장 가능한 인공지능 시스템을 구축하기 위해 우리는 오케스트레이션으로 전환해야 합니다. 이는 여러 개의 LLM 호출을 연결하고, 사용자 입력에 기반해 분기 로직을 설계하며, 모델이 외부 데이터와 상호작용할 수 있도록 하는 것을 포함합니다.
오케스트레이션의 기본 구성 요소
- LLMChain: 핵심 단위입니다. 프롬프트 템플릿과 언어 모델을 결합합니다.
- 순차적 체인: 여러 단계의 워크플로우를 만들 수 있게 해줍니다. 여기서 한 단계의 출력은 다음 단계의 입력이 됩니다.
- 라우터 체인: 이들은 '교통 통제자'처럼 작용하며, 특정 요청을 처리할 적절한 전문 서브체인을 선택하는 데 언어 모델을 사용합니다 (예: 수학 문제를 '수학 체인'으로 보내고 역사 질문을 '역사 체인'으로 보냅니다).
핵심 원칙: 체인 규칙
체인은 여러 구성 요소—모델, 프롬프트, 메모리—를 하나의 일관된 애플리케이션으로 결합할 수 있게 해줍니다. 이러한 모듈성 덕분에 복잡한 작업을 관리하기 쉬운, 디버깅 가능한 단계로 나눌 수 있습니다.
💡 프로 팁: 파이프라인 디버깅
파이프라인이 복잡해질 경우,
langchain.debug = True를 사용하세요. 이 '엑스레이 시각'을 통해 체인의 모든 단계에서 실제로 전송되는 프롬프트와 받은 원시 출력을 정확히 확인할 수 있습니다.TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
Question 1
In LangChain, what is the primary difference between a
SimpleSequentialChain and a standard SequentialChain?Challenge: Library Support Router
Design a routing mechanism for a specialized bot.
You are building a support bot for a library.
Define the logic for a
Define the logic for a
RouterChain that distinguishes between "Book Recommendations" and "Operating Hours."
Step 1
Create two prompt templates: one for book suggestions and one for library schedule info.
Solution:
book_template = """You are a librarian. Recommend books based on: {input}"""
schedule_template = """You are a receptionist. Answer hours queries: {input}"""
prompt_infos = [
{"name": "books", "description": "Good for recommending books", "prompt_template": book_template},
{"name": "schedule", "description": "Good for answering operating hours", "prompt_template": schedule_template}
]Step 2
Define the
router_template to guide the LLM on how to classify the user's intent, and initialize the chain.Solution:
router_template = MULTI_PROMPT_ROUTER_TEMPLATE.format(
destinations=destinations_str
)
router_prompt = PromptTemplate(
template=router_template,
input_variables=["input"],
output_parser=RouterOutputParser(),
)
router_chain = LLMRouterChain.from_llm(llm, router_prompt)
chain = MultiPromptChain(
router_chain=router_chain,
destination_chains=destination_chains,
default_chain=default_chain,
verbose=True
)